package com.company.pangu.aliyun.live.generator;

import com.company.pangu.aliyun.live.enums.PullProtocolType;
import com.company.pangu.aliyun.live.enums.StreamBitRateType;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;
import java.text.MessageFormat;

/**
 * <p>播流地址构造器</p >
 * https://help.aliyun.com/document_detail/199339.html
 *
 * @author youyoukele
 * @version 1.0
 * @date 2021/10/28
 */
@Builder
@Data
public class PullUrlGenerator implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * RTMP：rtmp://播流域名/{live}/{3}?鉴权串
     * FLV：http://播流域名/{live}/{3}.flv?鉴权串
     * HLS：http://播流域名/{live}/{3}.m3u8?鉴权串
     * UDP：artc://播流域名/{live}/{3}?鉴权串
     */
    private static final String FORMATTER = "{0}{1}/{2}/{3}?auth_key={4}";

    /**
     * 拉流协议
     */
    private PullProtocolType pullProtocolType;

    /**
     * 拉流域名
     */
    private String pullDomain;

    /**
     * 应用名
     */
    private String appName;

    /**
     * 直播流
     */
    private String streamName;

    /**
     * 流分辨率
     */
    private StreamBitRateType streamBitRateType;

    /**
     * 鉴权key
     * {@link AuthKeyGenerator}
     */
    private String authKey;

    /**
     * 生成直播拉流地址
     *
     * 播放地址支持RTMP、FLV、HLS、UDP格式，UDP格式即低延迟直播RTS使用地址，需提前开通RTS服务。不同的转码需要在StreamName后加_模板ID
     * RTMP：rtmp://play.a.com/app/stream_sd?auth_key=160871****-0-0-4c027af1df9237568c7035c290b0****
     * FLV：http://play.a.com/app/stream_sd.flv?auth_key=160871****-0-0-4c027af1df9237568c7035c290b0****
     * HLS：http://play.a.com/app/stream_sd.m3u8?auth_key=160871****-0-0-4c027af1df9237568c7035c290b0****
     * UDP：artc://play.a.com/app/stream_sd?auth_key=160871****-0-0-4c027af1df9237568c7035c290b0****
     *
     * @return
     */
    public String gen() {
        String finalStreamName = streamName;
        if (StreamBitRateType.ORIGIN == streamBitRateType) {
            if (PullProtocolType.RTMP == pullProtocolType) {
                finalStreamName = streamName;
            } else if (PullProtocolType.FLV == pullProtocolType) {
                finalStreamName = streamName + "." + pullProtocolType.getExtension();
            } else if (PullProtocolType.HLS == pullProtocolType) {
                finalStreamName = streamName + "." + pullProtocolType.getExtension();
            }
        } else {
            if (PullProtocolType.RTMP == pullProtocolType) {
                finalStreamName = streamName + "_" + streamBitRateType.getCode();
            } else if (PullProtocolType.FLV == pullProtocolType) {
                finalStreamName = streamName + "_" + streamBitRateType.getCode() + "." + pullProtocolType.getExtension();
            } else if (PullProtocolType.HLS == pullProtocolType) {
                finalStreamName = streamName + "_" + streamBitRateType.getCode() + "." + pullProtocolType.getExtension();
            }
        }
        return MessageFormat.format(PullUrlGenerator.FORMATTER, pullProtocolType.getNetProtocol(), pullDomain, appName, finalStreamName, authKey);
    }

    public static void main(String[] args) {
        PullUrlGenerator pullUrlGenerator = PullUrlGenerator.builder()
                .appName("app")
                .streamName("stream")
                .pullProtocolType(PullProtocolType.HLS)
                .pullDomain("pull.abcd.vip")
                .authKey("authKey")
                .streamBitRateType(StreamBitRateType.ORIGIN)
                .build();
        System.out.println(pullUrlGenerator.gen());
    }

}
